﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress10;
using HouseRental.DataSets;

namespace HouseRental.Controls
{
    public partial class EstimateAnalysis : UserControl, IGridView
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set { _controller = value; }
        }
        #endregion

        public EstimateAnalysis()
        {
            InitializeComponent();
            FormatGridColumns();
            gridView.DoubleClick += GridViewDoubleClick;
            gridView.OptionsView.ShowFooter = true;
        }

        public void RefreshData(AnalysisFilter analysisFilter)
        {
            AnaliseOrcamentalDataSet dataSet = _controller.EstimateAnalysis(analysisFilter);
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Orcamento.TableName;
            FormatGridColumns();
        }

        #region Private Methods

        private void FormatGridColumns()
        {
            DevExpressHelper.SetColumnAsNumeric(colDesvioPercentagem);
            DevExpressHelper.SetColumnAsCurrency(colReal);
            DevExpressHelper.SetColumnAsCurrency(colOrcamentado);
            DevExpressHelper.SetColumnAsCurrency(colDesvio);
            var gridColumns = new List<GridColumn> { colReal, colOrcamentado, colDesvio };
            DevExpressHelper.ShowGroupSummaryAsCurrency(gridView, gridColumns);
        }

        #endregion

        void GridViewDoubleClick(object sender, EventArgs e)
        {
            if (!_controller.ExistsOpenedEstimate(this, "Deseja mesmo abrir o orçamento?"))
                return;
            var estimate = (AnaliseOrcamentalDataSet.OrcamentoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (estimate == null)
                return;
            _controller.ShowEstimate(estimate.Id);
        }

        #region IGridView

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "HouseRental:EstimateAnalysis"; }
        }

        #endregion
           
    }
}
